<?php
/**
 * Created by PhpStorm.
 * User: 江清月近人
 * Date: 2021/11/29
 * Time: 11:28
 */
namespace app\api\controller;


use app\admin\model\Address as AddressModel;
use app\common\controller\Api;
class Address extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    /**
     * 获取用户收货地址
     * 2021-09-13 17:03
     */
    public function index()
    {
        $address = new AddressModel();
        $data  = input('put');
        $data = $address
            ->where('user_id', $this->auth->id)
            ->field('id,name,phone,city,address,is_default')
            ->order('is_default asc')
            ->select();
        if ($data) {
            $this->success('请求成功', $data);
        }
            $this->error('暂无数据');
    }


    //添加用户收货地址
    public function create()
    {
        $postData = $this->request->post();
        $model = new AddressModel();
        $validate = new \app\api\validate\Address();
        if (!$validate->scene('add')->check($postData)) {
            $this->error($validate->getError());
        }
        $address = $model->where(['user_id' => $this->auth->id, 'is_default' => 1, 'status' => 1])->find();
        $model->user_id = $this->auth->id;
        if (isset($postData['is_default']) && $postData['is_default'] == 1 && !empty($address)) {
            $address['is_default'] = -1;
            $address->save();
        }
        if ($model->save($postData)) {
            $this->success('用户收货地址添加成功');
        } else {
            $this->error('添加失败');
        }

    }


    public function edit()
    {
        $id = input('id');
        if (empty($id)) {
            $this->error('请传入id');
        }
        $model = new AddressModel();
        $data = $model->field('id,name,phone,city,address,is_default')->find($id);
        if ($data) {
            $this->success('获取成功', $data);
        } else {
            $this->error('获取失败');
        }
    }

    //修改用户收货地址
    public function update()
    {
        $postData = $this->request->post();
        $model = new AddressModel();
        $validate = new \app\api\validate\Address();
        if (!$validate->scene('edit')->check($postData)) {
            $this->error($validate->getError());
        }
        $address = $model->where(['user_id' => $this->auth->id, 'is_default' => 1, 'status' => 1])->find();
        $model->user_id = $this->auth->id;
        if (isset($postData['is_default']) && $postData['is_default'] == 1 && !empty($address)) {
            $address['is_default'] = -1;
            $address->save();
        }
        if (AddressModel::update($postData)) {
            $this->success('用户收货地址修改成功');
        } else {
            $this->error('修改失败');
        }
    }

    //删除用户收货地址
    public function delete()
    {
        $ids = input('ids');
        if (empty($ids)) {
            $this->error('请选中需要删除的商品');
        }
        if (AddressModel::where('id', 'in', $ids)->delete()) {
            $this->success('删除成功');
        }
        $this->error('删除失败');
    }

    //用户默认地址
    public function address()
    {
        $address = new AddressModel();
        $data = $address
            ->where('user_id', $this->auth->id)
            ->where('is_default',1)
            ->field('id,name,phone,city,address,is_default')
            ->find();
        if (!empty($data)){
            $data->unsetAppend();
        }
        $this->success('请求成功', $data);

    }
}